package com.ts.opengl

import android.content.Context
import android.opengl.GLES20
import android.opengl.GLES30
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader

class OpenGLUtils {

    companion object{
        @JvmStatic
        fun readRawTextFile(context: Context, rawId: Int): String {
            val `is` = context.resources.openRawResource(rawId)
            val br = BufferedReader(InputStreamReader(`is`))
            var line: String?
            val sb = StringBuilder()
            try {
                while ((br.readLine().also { line = it }) != null) {
                    sb.append(line)
                    sb.append("\n")
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
            try {
                br.close()
            } catch (e: IOException) {
                e.printStackTrace()
            }
            return sb.toString()
        }
        @JvmStatic
        fun loadProgram( vSource:String,fSource:String):Int{
            /**
             *  片元着色器
             *  流程和上面一样
             */
            val fShard=GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER)
            //加载着色器
            GLES20.glShaderSource(fShard,fSource)
            //编译
            GLES20.glCompileShader(fShard)
            val status=IntArray(1)
            GLES20.glGetShaderiv(fShard,GLES20.GL_COMPILE_STATUS,status,0)
            check(status[0] == GLES20.GL_TRUE) {
                //失败
                "load vertex shader:" + GLES30.glGetShaderInfoLog(fShard)
            }



            //顶点着色器
            val vShard= GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER)
            //加载着色器
            GLES20.glShaderSource(vShard,vSource)
            //编译
            GLES30.glCompileShader(vShard)



            GLES30.glGetShaderiv(vShard,GLES30.GL_COMPILE_STATUS,status,0)
            check(status[0] == GLES30.GL_TRUE) {
                //失败
                "load vertex shader:" + GLES30.glGetShaderInfoLog(vShard)
            }




            val program=GLES30.glCreateProgram()
            //绑定顶点和片元
            GLES30.glAttachShader(program,vShard)

            GLES30.glAttachShader(program,fShard)

            GLES30.glLinkProgram(program)
            //获得状态
            GLES30.glGetProgramiv(program, GLES30.GL_LINK_STATUS, status, 0)
            check(status[0] == GLES30.GL_TRUE) {
                "link program:" + GLES30.glGetProgramInfoLog(
                    program
                )
            }
            GLES30.glDeleteShader(vShard)
            GLES30.glDeleteShader(fShard)
            return program


        }
        @JvmStatic
        fun glGenTextures(textures:IntArray){
            //生成文理
            GLES30.glGenTextures(textures.size,textures,0);

            for (texture in textures) {
                //绑定文理
                GLES30.glBindTexture(GLES30.GL_TEXTURE_2D,
                    texture)
                /**
                 *  必须：设置纹理过滤参数设置
                 */
                GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER,
                    GLES30.GL_LINEAR)

                GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER,
                    GLES30.GL_LINEAR)

                GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0)

            }


        }
    }
}